<template>
  <BasicModal v-bind="$attrs" @register="userRegister" :title="getTitle" width="900px" @ok="submitAudit">
    <BasicTable @register="registerUserTable" :loading="loading" :rowSelection="{ type: 'radio' }" :pagination="false"> </BasicTable>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { ref, reactive } from 'vue';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { handleUsers } from './LabEntrustAllotList.api';
  import { labUserColumns } from './LabEntrustAllotList.data';
  import { BasicTable } from '/@/components/Table';
  import { FormSchema } from '/@/components/Table';
  const loading = ref(false);
  const getTitle = ref('可选实验人员');
  const formData = reactive({
    mId: '',
    type: '',
    realname:'',
  });
  let flagCheckPerson = ref('');
  const emits = defineEmits<{
    (event: 'getSelectResult', uId: String, uName: String): void;
    (event: 'getSelectUser', uId: String, uName: String): void;
    (event: 'selectAuditConfirm', uId: String, uName: String): void;
    (event: 'selectAuditConfirm2', uId: String, uName: String): void;
  }>();

  const [userRegister, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({
      minHeight: 180,
    });
    formData.mId = data.mId;
    formData.type = data.type;
    formData.realname = data.realname;
    flagCheckPerson.value = data.flag;
    // loading.value = true;
    reload();
  });
  const searchFormSchema: FormSchema[] = [
    {
      label: '姓名',
      field: "formData.realname",
      component: 'Input',
      colProps: { span: 6 },
    },
  ];
  //注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      api: handleUsers,
      columns: labUserColumns,
      canResize: false,
      formConfig: {
        schemas: searchFormSchema,
      },
      showIndexColumn: true,
      showTableSetting: false,
      showActionColumn: false,
      beforeFetch: (params) => {
        params.pageSize = 100;
        if(params.formData){
          params.realname2 = params.formData.realname;
          return Object.assign(params, { mId: formData.mId,realname:params.realname2 });
        }else {
          return Object.assign(params, { mId: formData.mId });
        };
      },
    },
  });

  const [registerUserTable, {reload, getSelectRows }] = tableContext;

  async function submitAudit() {
    const selectedRows = getSelectRows()[0];
    const id = selectedRows['id'];
    const realname = selectedRows['realname'];
    if (flagCheckPerson.value === 'audit') {
      emits('selectAuditConfirm', id, realname);
    }else if(flagCheckPerson.value === 'min'){
      emits('selectAuditConfirm2', id, realname);
    }else {
      if (formData.type == 'all') {
        emits('getSelectResult', id, realname);
      } else {
        emits('getSelectUser', id, realname);
        
      }
    }

    closeModal();
  }
</script>
